﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Linq;
using ReportLib;

namespace ReportService
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class CrystalReportService : ICrystalReportService
    {
        //http://www.codeproject.com/Tips/74499/Crystal-Report-in-WPF.aspx
        #region ICrystalReportService Members

        public List<ReportSummary> GetReportList(ReportEnquiry enquiry)
        {
            var reportList=  Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.rpt").
                Select(
                x => 
                {
                    return  new ReportSummary { ReportName = x };
                }).ToList();

            return reportList;
        }

        public ReportDetails GetCrystalReportDetails(ReportSummary reportSummary)
        {
            ReportDetails reportDetails =new ReportDetails();

            ReportDocument reportDocument = new ReportDocument();

            reportDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "\\" + reportSummary.FolderName + reportSummary.ReportName);

            ParameterFields parameterFields = reportDocument.ParameterFields;

            reportDetails.Parameters = new List<ReportParameter>();

            if (parameterFields != null)
            {              
                foreach (ParameterField parameterField in parameterFields )
                {
                    reportDetails.Parameters.Add( new ReportParameter{ Name =parameterField.Name , Type = parameterField.ParameterValueType.ToString() });
                }
            }

            return reportDetails;
        }

        public ReportDetails LoadReport(ReportDetails crystalReport)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
